package com.example.lsn_opengles713;

import android.opengl.GLU;

import com.example.lsn_opengles713.util.BufferUtil;

import javax.microedition.khronos.opengles.GL10;

/**
 * 立方体渲染器,线代模式,正交投影
 */
public class MyCubeRenderer extends AbstractMyRenderer {

	/**
	 * 2.
	 */
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		//设置视口
		gl.glViewport(0, 0, width, height);
		ratio = (float)width / (float)height;
		//投影矩阵
		gl.glMatrixMode(GL10.GL_PROJECTION);
		//加载单位矩阵
		gl.glLoadIdentity();
		//设置平截头体
		gl.glOrthof(-ratio, ratio, -1, 1, 3f, 7f);
	}
	
	public void onDrawFrame(GL10 gl) {
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		gl.glColor4f(0f, 0f, 1f, 1f);
		
		//模型视图矩阵
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();
		GLU.gluLookAt(gl, 0, 0, 5, 0, 0, 0, 0, 1, 0);
		
		//旋转
		gl.glRotatef(xrotate, 1, 0, 0);
		gl.glRotatef(yrotate, 0, 1, 0);
		
		float r = 0.4f ;
		//定义点坐标
		float[] coords = {
			-r,r,r, //front left up
			-r,-r,r,//front let bottom
			r,r,r,  //front right up
			r,-r,r, //front right bottom
			
			-r,r,-r, //back left up
			-r,-r,-r,//back let bottom
			r,r,-r,  //back right up
			r,-r,-r, //back right bottom
		};
		
		//顶点的索引顺序
		byte[] indices = {
			0,1,5,4,0,
			0,4,6,2,
			6,7,3,
			7,5,1,
			0,2,3,1
		};
		
		gl.glColor4f(1f, 0f, 0f, 1f);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, BufferUtil.arr2FloatBuffer(coords));
		gl.glDrawElements(GL10.GL_LINE_STRIP, indices.length, GL10.GL_UNSIGNED_BYTE, BufferUtil.arr2ByteBuffer(indices));
	}
}
